clear 

* set directory 
* cd "directory"

* set paths
global datafolder "data"
global outputfolder "output"

********************************************************************************
*			Table 6: Introduction of new pricing guidelines
********************************************************************************

	use "$datafolder\maindata.dta", clear
	
	sort customerid datem
	
	* AR change 2015-2014
	bysort customerid year: egen ar_yr = mean(actual_rate)
	
	gen ar_2015_x = ar_yr if year==2015
	bysort customerid: egen ar_2015 = mean(ar_2015_x)
	drop ar_2015_x
	
	gen ar_2014_x = ar_yr if year==2014
	bysort customerid: egen ar_2014 = mean(ar_2014_x)
	drop ar_2014_x
	
	gen dif_ar_1415 = ar_2015 - ar_2014
	
	* Dif(ThR2015 - AR2014)
	bysort customerid year: egen thr_yr = mean(theoretical_rate)
	
	gen thr_2015_x = thr_yr if year==2015
	bysort customerid: egen thr_2015 = mean(thr_2015_x)
	drop thr_2015_x
	
	gen dif_thr_15 = thr_2015 - ar_2014
	
	
	* Create differences for OROA, deposits, asets, and liab/assets
	rename ratio_CurrentLiabilities ratio_CurrLiab
	
	foreach var of varlist OROA lDeposits lTotalAssets ratio_CurrLiab{
		gen `var'_2015_x = `var' if year==2015
		bysort customerid: egen `var'_2015 = mean(`var'_2015_x)
		
		gen `var'_2014_x = `var' if year==2014
		bysort customerid: egen `var'_2014 = mean(`var'_2014_x)
		
		gen dif_`var'_1415 = `var'_2015 - `var'_2014
		drop `var'_2014_x `var'_2015_x
	}
	
	drop if dif_ar_1415 == . | dif_thr_15 == .
	
	keep if year==2015
	
	global cvars "OROA lDeposits lTotalAssets ratio_CurrLiab"
	global cvars_dif "dif_OROA_1415 dif_lDeposits_1415 dif_lTotalAssets_1415 dif_ratio_CurrLiab_1415"
	
	collapse (mean) dif_ar_1415 dif_thr_15 $cvars  ///
	ar_2015 ar_2014 thr_2015 Industry $cvars_dif, by(customerid)
		
	label variable dif_ar_1415 "AR\$_{2015}$ - AR\$_{2014}$"
	label variable dif_thr_15 "Breakeven\$_{2015}$ - AR\$_{2014}$"
	label variable ar_2015 "Actual Rate\$_{2015}$"
	label variable ar_2014 "Actual Rate\$_{2014}$"
	label variable thr_2015 "Breakeven\$_{2015}$"
	label variable OROA "OROA"
	label variable lDeposits "Deposits"
	label variable lTotalAssets "Assets"
	label variable ratio_CurrLiab "Liabilities/Assets"
	label variable dif_OROA_1415 "$\Delta$ OROA\$_{2015}$"
	label variable dif_lDeposits_1415 "$\Delta$ Deposits\$_{2015}$"
	label variable dif_lTotalAssets_1415 "$\Delta$ Assets\$_{2015}$"
	label variable dif_ratio_CurrLiab_1415 "$\Delta$ Liabilities/Assets\$_{2015}$"
	
	
	estimates clear
	
	reg dif_ar_1415 dif_thr_15, robust
	quietly estadd local hasind "No"
	estimates store M1
	
	reg dif_ar_1415 dif_thr_15 $cvars_dif, robust
	quietly estadd local hasind "No"
	estimates store M2
	
	reghdfe dif_ar_1415 dif_thr_15 $cvars_dif, abs(Industry) vce(robust)
	quietly estadd local hasind "Yes"
	estimates store M3
	
	
	esttab * using "$outputfolder\table_6_A.tex", replace substitute(\_ _) ///
	se r2 label star(* 0.1 ** 0.05 *** 0.01) nonotes noomitted /// 
	b(3) se(3) r2(3) ///
	noconstant scalars("hasind Industry FE") nomtitles ///
	mgroups("AR\$_{2015}$ - AR\$_{2014}$", ///
	pattern(1 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) ///
	span erepeat(\cmidrule(lr){@span}))
	
	
	
	estimates clear
	
	reg ar_2015 thr_2015 ar_2014 , robust
	quietly estadd local hasind "No"
	estimates store M1
	
	reg ar_2015 thr_2015 ar_2014  $cvars, robust
	quietly estadd local hasind "No"
	estimates store M2
	
	reghdfe ar_2015  thr_2015 ar_2014  $cvars, abs(Industry) vce(robust)
	quietly estadd local hasind "Yes"
	estimates store M3
	
	esttab * using "$outputfolder\table_6_B.tex", replace substitute(\_ _) ///
	se r2 label star(* 0.1 ** 0.05 *** 0.01) nonotes noomitted /// 
	b(3) se(3) r2(3) ///
	noconstant scalars("hasind Industry FE") nomtitles ///
	mgroups("Actual Rate\$_{2015}$", ///
	pattern(1 0 0 1 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) ///
	span erepeat(\cmidrule(lr){@span}))
	